﻿using SkiaSharp;
public partial class Program
{
    /// <summary>
    /// 手工矩阵方式创建一张图片，然后水平翻转保存为jpg
    /// </summary>
    static void SkiaSharpTest()
    {
        // https://docs.microsoft.com/zh-cn/dotnet/api/skiasharp?view=skiasharp-2.80.3
        var buffer = new byte[600 * 400];
        for (int i = 0; i < 600; i++)
        {
            for (int j = 0; j < 400; j++)
            {
                if (i < 300 && j < 200)
                {
                    buffer[i * 400 + j] = 50;
                }
                if (i < 300 && j > 200)
                {
                    buffer[i * 400 + j] = 100;
                }
                if (i > 300 && j < 200)
                {
                    buffer[i * 400 + j] = 150;
                }
                if (i > 300 && j > 200)
                {
                    buffer[i * 400 + j] = 200;
                }
            }
        }
        var imgInfo = new SKImageInfo(400, 600, SKColorType.Gray8);
        var img = SKImage.FromPixelCopy(imgInfo, buffer);
        var surface = SKSurface.Create(imgInfo);
        var canvas = surface.Canvas;
        canvas.Scale(-1, 1, 200, 0);
        // 可以画很多种元素，包括DrawBitmap，DrawRect，DrawOval，DrawPath，DrawText等
        canvas.DrawImage(img, 0, 0);
        var jpgData = surface.Snapshot().Encode(SKEncodedImageFormat.Jpeg, 100).ToArray();
        File.WriteAllBytes(@"E:\abc.jpg", jpgData);
    }
}